#import "ImageView.h"

#import "Controller.h"

@implementation ImageView

//------------------------------------------------------------------------------
- (id)initWithFrame:(NSRect) frame
{
	if (self = [super initWithFrame:frame])
	{
		m_nameAttrs = [[NSMutableDictionary alloc] initWithObjectsAndKeys: nil];
		m_scale = YES;
		m_ptImage = NSMakePoint(0, 0);

		[self setMenu: [[Controller sharedController] imageMenu]];
	}

	return self;
}

//------------------------------------------------------------------------------
- (void) setBackgroundColor:(NSColor*) newColor
{
	[m_nameAttrs setObject: newColor  forKey: NSBackgroundColorAttributeName];
}

//------------------------------------------------------------------------------
- (void) setForegroundColor:(NSColor*) newColor
{
	if (nil == newColor)
	{
		[m_nameAttrs removeObjectForKey: NSForegroundColorAttributeName];
	}
	else
	{
		[m_nameAttrs setObject: newColor  forKey: NSForegroundColorAttributeName];
	}
}

//------------------------------------------------------------------------------
- (void)drawRect:(NSRect) rect
{
	[super drawRect: rect];

	NSRect view = [self bounds];
	if (nil != m_image)
	{
		NSRect viewRect = [self bounds];
		NSRect imgRect = { NSMakePoint(0, 0), m_szImage};

		[m_image setSize: m_szImage];

		if (NSHeight(imgRect) > NSHeight(viewRect) ||
			NSWidth(imgRect) > NSWidth(viewRect))
		{
			float vw = NSWidth(imgRect) * NSHeight(viewRect) / NSHeight(imgRect);
			float vh = NSHeight(imgRect) * NSWidth(viewRect) / NSWidth(imgRect);
			if (vh < NSHeight(viewRect))
			{
				viewRect.size.height = vh;
			}
			else
			{
				viewRect.size.width = vw;
			}
		}
		else
		{
			viewRect.size.height = imgRect.size.height;
			viewRect.size.width = imgRect.size.width;
		}

		if (viewRect.size.width < view.size.width)
		{
			viewRect.origin.x = (view.size.width - viewRect.size.width) / 2;
		}

		if (viewRect.size.height < view.size.height)
		{
			viewRect.origin.y = (view.size.height - viewRect.size.height) / 2;
		}

		if (NO == m_scale)
		{
			imgRect.size.width *= 0.90;
			imgRect.size.height *= 0.90;
		}

		[m_image drawInRect:viewRect fromRect:imgRect operation:NSCompositeCopy fraction:1.0];
	}

	if (nil != m_imageName)
	{
		NSSize sizeString = [m_imageName sizeWithAttributes: m_nameAttrs];
		[m_imageName drawAtPoint:
			NSMakePoint(1, view.size.height - sizeString.height) 
			withAttributes: m_nameAttrs];
	}

	if (nil != m_imageIndex)
	{
		NSSize sizeString = [m_imageIndex sizeWithAttributes: m_nameAttrs];
		[m_imageIndex drawAtPoint:
			NSMakePoint(
				view.size.width - sizeString.width - 1, 
				view.size.height - sizeString.height) 
			withAttributes:m_nameAttrs];
	}

	if (nil != m_imageDate)
	{
		[m_imageDate drawAtPoint:
			NSMakePoint(1, 0)
			withAttributes: m_nameAttrs];
	}
}

//------------------------------------------------------------------------------
- (void) setImage:(NSImage*) image
{
	[m_image release];
	m_image = image;
	if (nil != m_image)
	{
		[m_image retain];
		NSImageRep *rep = [m_image bestRepresentationForDevice: nil];
		m_szImage.width = [rep pixelsWide];
		m_szImage.height = [rep pixelsHigh];
	}
}

//------------------------------------------------------------------------------
- (void) setImageName:(NSString*) name
{
	[m_imageName release];
	if (nil != name)
	{
		m_imageName = [NSString stringWithString:name];
		[m_imageName retain];
	}
	else
	{
		m_imageName = nil;
	}
}

//------------------------------------------------------------------------------
- (void) setImageIndex:(NSString*) index
{
	[m_imageIndex release];
	if (nil != index)
	{
		m_imageIndex = [NSString stringWithString:index];
		[m_imageIndex retain];
	}
	else
	{
		m_imageIndex = nil;
	}
}

//------------------------------------------------------------------------------
- (void) setImageDate:(NSString*) date
{
	[m_imageDate release];
	if (nil != date)
	{
		m_imageDate = [NSString stringWithString: date];
		[m_imageDate retain];
	}
	else
	{
		m_imageDate = nil;
	}
}

//------------------------------------------------------------------------------
- (void) setScaling:(BOOL) scaling
{
	m_scale = scaling;
	[self setNeedsDisplay: YES];
}

//------------------------------------------------------------------------------
- (NSSize) getImageSize
{
	return m_szImage;
}

//------------------------------------------------------------------------------
- (id) delegate
{
	return delegate;
}

//------------------------------------------------------------------------------
- (void) setDelegate: (id) newDelegate
{
	delegate = newDelegate;
}

//------------------------------------------------------------------------------
// Forward all keys to our delegate, if we have one.
- (void) keyDown:(NSEvent *) evt
{
	if ([delegate respondsToSelector:@selector(imageView: keyDown:)])
	{
		[delegate imageView: self  keyDown: evt];
	}
}

//------------------------------------------------------------------------------
-(void)mouseDown:(NSEvent *)event
{
	[[self window] setAcceptsMouseMovedEvents:YES];
	[[self window] invalidateCursorRectsForView:self];
}

//------------------------------------------------------------------------------
-(void)mouseUp:(NSEvent *)event
{
	[[self window] setAcceptsMouseMovedEvents:NO];
	[[self window] invalidateCursorRectsForView:self];
}

//------------------------------------------------------------------------------
-(void)mouseDragged:(NSEvent *)event
{
	NSPoint startOrigin = [(NSClipView*)[self superview] documentVisibleRect].origin;
	NSPoint newOrigin = NSMakePoint(startOrigin.x - [event deltaX], startOrigin.y + [event deltaY]);
	[self scrollPoint: newOrigin];
}

//------------------------------------------------------------------------------
- (NSDragOperation) draggingEntered:(id <NSDraggingInfo>) sender
{
	return [[Controller sharedController] draggingEnteredHelper: sender];
}

//------------------------------------------------------------------------------
- (BOOL) prepareForDragOperation:(id <NSDraggingInfo>) sender
{
	return [[Controller sharedController] prepareForDragOperationHelper: sender];
}

//------------------------------------------------------------------------------
- (BOOL) performDragOperation:(id <NSDraggingInfo>) sender
{
	return [[Controller sharedController] performDragOperationHelper: sender];
}

//------------------------------------------------------------------------------
- (void) dealloc
{
	[m_nameAttrs release];
	[m_image release];
	[m_imageName release];
	[m_imageIndex release];
	[m_imageDate release];

	[super dealloc];
}

@end
